home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / ipc / sub_servfd.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  949b  |  46 lines

  1. #include    <stdio.h>
  2.  
  3. #define    MAXBUFF        1024
  4.  
  5. server(readfd, writefd)
  6. int    readfd;
  7. int    writefd;
  8. {
  9.     char        buff[MAXBUFF];
  10.     char        errmesg[256], *sys_err_str();
  11.     int        n, fd;
  12.     extern int    errno;
  13.  
  14.     /*
  15.      * Read the filename from the IPC descriptor.
  16.      */
  17.  
  18.     if ( (n = read(readfd, buff, MAXBUFF)) <= 0)
  19.         err_sys("server: filename read error");
  20.     buff[n] = '\0';        /* null terminate filename */
  21.  
  22.     if ( (fd = open(buff, 0)) < 0) {
  23.         /*
  24.          * Error.  Format an error message and send it back
  25.          * to the client.
  26.          */
  27.  
  28.         sprintf(errmesg, ": can't open, %s\n", sys_err_str());
  29.         strcat(buff, errmesg);
  30.         n = strlen(buff);
  31.         if (write(writefd, buff, n) != n)
  32.             err_sys("server: errmesg write error");
  33.     } else {
  34.         /*
  35.          * Read the data from the file and write to
  36.          * the IPC descriptor.
  37.          */
  38.  
  39.         while ( (n = read(fd, buff, MAXBUFF)) > 0)
  40.             if (write(writefd, buff, n) != n)
  41.                 err_sys("server: data write error");
  42.         if (n < 0)
  43.             err_sys("server: read error");
  44.     }
  45. }
  46.